﻿var _pendingAjaxRequests = 0;//todo: merge the logic embedded in PendingRequest.js

function RequestManager(request, responseDelegate, ignoreProcessing, errorDelegate) {
    this.Request = request;
    this.ResponseDelegate = responseDelegate;
    this.IgnoreProcessing = ignoreProcessing === undefined ? false : ignoreProcessing;
    this.ErrorDelegate = errorDelegate;
}

RequestManager.prototype.ExecuteRequest = function () {
    setPageScroll($("#bodyContent").scrollTop());

    if (!this.IgnoreProcessing) {
        _pendingAjaxRequests++;
        ShowProcessing();
    }

    var responseDelegate = this.ResponseDelegate; //"this" has different scope inside the success call back
    var errorDelegate = this.ErrorDelegate;
    $.ajax({
        type: "POST",
        url: document.URL,
        data: "'" + JSON.stringify(this.Request) + "'",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (jsonResponse) {
            if (!jsonResponse)
                return;
            if (jsonResponse.ErrorMessage == null &&
                responseDelegate !== undefined) {
                responseDelegate(jsonResponse);
            }
            else if (jsonResponse.ErrorMessage !== null &&
                $status !== undefined &&
                $status.failures !== undefined) {
                failure = { "message": jsonResponse.ErrorMessage };
                $status.failures.push(failure);
                _ignoreServerStatuses = true;
                ToggleStatuses();
                _ignoreServerStatuses = false;
                if (errorDelegate !== undefined) {
                    errorDelegate(jsonResponse);
                }
            }
            else if (jsonResponse.ErrorMessage !== null &&
                errorDelegate !== undefined) {
                errorDelegate(jsonResponse);
            }

            if (!this.IgnoreProcessing) {
                _pendingAjaxRequests--;
                if (_pendingAjaxRequests === 0) {
                    HideProcessing();
                }
            }
            goToScroll();
        },
        fail: function (xhr) {
            //todo: create fail status , send it to the global collection, toggle statuses
        },
        error: function (jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        }
    });
}